﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;

namespace MyWebPagesStarterKit
{
    /// <summary>
    /// Summary description for Discussion
    /// </summary>
    public class Discussion : Section<Discussion.Data>
    {
        public Discussion()
        {
            _data.Entries = new DataSet();
            _data.Entries.Tables.Add(new DataTable("Entries"));

            DataColumn primaryKeyColumn = new DataColumn("Guid", typeof(Guid));
            primaryKeyColumn.Unique = true;

            _data.Entries.Tables["Entries"].Columns.Add(primaryKeyColumn);
            _data.Entries.Tables["Entries"].PrimaryKey = new DataColumn[] { primaryKeyColumn };

            _data.Entries.Tables["Entries"].Columns.AddRange(
                new DataColumn[] 
                {
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Comment", typeof(string)),
                    new DataColumn("DateAdded", typeof(DateTime)),
                    new DataColumn("Tracker", typeof(string))
                }
           );
        }

        public Discussion(string id) : base(id) { }

        public override DataTable Entries
        {
            get { return _data.Entries.Tables["Entries"]; }
        }

        public DataView GetEntries()
        {
            return new DataView(Entries, string.Empty, "DateAdded DESC", DataViewRowState.CurrentRows);
        }


        public DataRow GetLinkEntry(string id)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        public override List<SearchResult> Search(string searchString, WebPage page)
        {
            List<SearchResult> foundResults = new List<SearchResult>();
            foreach (DataRow row in Entries.Rows)
            {
                row["Comment"] = HttpUtility.HtmlDecode(SearchResult.RemoveHtml(row["Comment"].ToString()));
            }
            DataRow[] foundRows = Entries.Select(string.Format("Comment LIKE '%{0}%'", searchString.Replace("'", "''"), searchString));
            foreach (DataRow row in foundRows)
            {
                foundResults.Add(new SearchResult(
                    string.Format("~/Default.aspx?pg={0}#{1}", page.PageId, SectionId),
                    (string)row["Name"],
                    SearchResult.CreateExcerpt((string)row["Comment"], searchString))
                );
            }
            return foundResults;
        }

        public override bool IsOnline
        {
            get
            {
                return _data.IsOnline;
            }
            set
            {
                _data.IsOnline = value;
            }
        }

        public override Position Position
        {
            get
            {
                return _data.Position;
            }
            set
            {
                _data.Position = value;
            }
        }

        public string Title
        {
            get { return _data.Title; }
            set { _data.Title = value; }
        }
        
        public class Data
        {
            public DataSet Entries;
            public bool IsOnline;
            public string Title;
            public Position Position;
        }
    }
}
